package com.pengpai.pangucms.controller;

import cn.hutool.core.util.StrUtil;
import com.pengpai.pangucms.VO.MenuVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pengpai.pangucms.entity.SysPermission;
import com.pengpai.pangucms.entity.SysUser;
import com.pengpai.pangucms.service.ISysPermissionService;
import com.pengpai.pangucms.utils.R;
import com.pengpai.pangucms.utils.ResultTree;
import com.pengpai.pangucms.utils.UserUtils;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.time.LocalDateTime;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author lijunjie
 * @since 2023-07-30
 */
@Controller
@RequestMapping("/sysPermission")
public class SysPermissionController {


    @Resource
    private ISysPermissionService sysPermissionService;

    @GetMapping("/tree")
    @ResponseBody
    public List<MenuVO> getSysPermissionTree(){
        return sysPermissionService.getMenuTree();
    }

    @GetMapping("/index")
    @PreAuthorize("hasAuthority('sys:permission:read')")
    public String index(){
        return "admin/sys_permission/sys_permission_index";
    }

    @GetMapping("list")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:permission:read')")
    public Map<String,Object> list(){
        List<SysPermission> sysPermissionList = sysPermissionService.list();
        Collections.sort(sysPermissionList, new Comparator<SysPermission>() {
            @Override
            public int compare(SysPermission o1, SysPermission o2) {
                return o1.getOrderId() - o2.getOrderId();
            }
        });
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","");
        map.put("count", sysPermissionList.size());
        map.put("data",sysPermissionList);

        return map;
    }

    @GetMapping("add")
    @PreAuthorize("hasAuthority('sys:permission:create')")
    public String add(String pid, Model model){
        model.addAttribute("pid", pid);
        return "admin/sys_permission/sys_permission_add";
    }

    @GetMapping("selectParent")
    @ResponseBody
    public ResultTree selectParent(SysPermission sysPermission) {
        List<SysPermission> list = sysPermissionService.list();
        SysPermission basePower = new SysPermission();
        basePower.setPermissionName("顶级权限");
        basePower.setPermissionId(0);
        basePower.setParentId(-1);
        list.add(basePower);
        ResultTree tree = new ResultTree();
        tree.setData(list);
        return tree;
    }

    @PostMapping("create")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:permission:create')")
    public R create(SysPermission sysPermission){
        String icon = sysPermission.getIcon();
        if(icon.indexOf("layui-icon ") == -1){
            icon = "layui-icon " + icon;
            sysPermission.setIcon(icon);
        }
        sysPermission.setCreateUser(UserUtils.CurrentUserName());
        sysPermission.setCreateTime(LocalDateTime.now());
        sysPermission.setUpdateUser(UserUtils.CurrentUserName());
        sysPermission.setUpdateTime(LocalDateTime.now());

        sysPermissionService.save(sysPermission);
        return R.success();
    }

    @PostMapping("/del")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:permission:delete')")
    public R del(String ids){
        List<Long> list = null;
        if(StrUtil.isNotBlank(ids)){
            list = Stream.of(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        sysPermissionService.removeByIds(list);
        return R.success();
    }

    @GetMapping("/edit")
    @PreAuthorize("hasAuthority('sys:permission:update')")
    public String edit(String id, Model model){
        SysPermission sysPermission = sysPermissionService.getById(id);
        if(sysPermission.getParentId() == 0){
            sysPermission.setRemark("顶级权限");
        }else{
            SysPermission parent = sysPermissionService.getById(sysPermission.getParentId());
            sysPermission.setRemark(parent.getPermissionName());
        }
        model.addAttribute("pm", sysPermission);
        return "admin/sys_permission/sys_permission_edit";
    }

    @PostMapping("update")
    @ResponseBody
    @PreAuthorize("hasAuthority('sys:permission:update')")
    public R update(SysPermission sysPermission){
        String icon = sysPermission.getIcon();
        if(icon.indexOf("layui-icon ") == -1){
            icon = "layui-icon " + icon;
            sysPermission.setIcon(icon);
        }
        sysPermission.setUpdateUser(UserUtils.CurrentUserName());
        sysPermission.setUpdateTime(LocalDateTime.now());
        sysPermissionService.updateById(sysPermission);
        return R.success("菜单信息更新成功！");
    }

}
